/**
 * @Title: ConfClock.java
 * @Package com.kaver.utils
 * @author roc
 * @date 2019年12月19日 上午9:35:58
 * @version V1.0
 * @copyright kaver
 */
package com.kaver.utils;

import java.io.IOException;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kaver.constant.PropertyConstant;
import com.kaver.spring.LocalCacheRepository;

/**
 * @ClassName: ConfClock
 * @Description: 配置中心时钟
 * @author roc
 * @date 2019年12月19日 上午9:35:58
 * 
 */
public class ConfClock {
    private final static Logger logger = LoggerFactory.getLogger(LocalCacheRepository.class);
    
    private static final String LCA_URL_TEST = "http://192.168.10.241:10020/tnq/v1/sysDateTime";
    private static final String LCA_URL_DEV = "http://192.168.10.103:10020/tnq/v1/sysDateTime";
    private static final String LCA_URL_UAT = "http://192.168.10.122:10020/tnq/v1/sysDateTime";
    private static final String LCA_URL_PRE = "http://lca-provider.stage.kaver.com/tnq/v1/sysDateTime";
    
    private static final String LSA_URL_DEV = "http://192.168.10.103:8903/api/lsa/v1/bizDate";
    private static final String LSA_URL_TEST = "http://lsa.t1.kaver.net/api/lsa/v1/bizDate";
    private static final String LSA_URL_UAT = "http://lsa.t2.kaver.net/api/lsa/v1/bizDate";
    private static final String LSA_URL_PRE = "http://account-api.lxh.kaver.com/api/lsa/v1/bizDate";
    
    public static long currentTimeMillis() {
        try {
            String group = LocalCacheRepository.getInstance().get("bizTime.group");
            String env = System.getProperty(PropertyConstant.SPRING_PROFILES_ACTIVE);
            if ("lsa".equals(group)) {
                if (env != null && !PropertyConstant.CONF_PROD_ENV.equals(env)) {
                    long biztime = getLsaBizTime();
                    return biztime;
                }
            } else {
                if (env != null && !PropertyConstant.CONF_PROD_ENV.equals(env)) {
                    long biztime = getLcaBizTime();
                    return biztime;
                }
            }
        } catch (Exception e) {
            logger.error("获取业务时间失败", e);
        }
        return System.currentTimeMillis();
    }

    public static Long getLcaBizTime() {
        JSONObject json = new JSONObject();
        json.put("organization", "kaver");
        json.put("exSystem", "CGW");
        String env = System.getProperty(PropertyConstant.SPRING_PROFILES_ACTIVE);
        try {
            String lcaUrl = "";
            if (PropertyConstant.CONF_PROD_DEV.equals(env)) {
                lcaUrl = LCA_URL_DEV;
            } else if (PropertyConstant.CONF_PROD_TEST.equals(env)) {
                lcaUrl = LCA_URL_TEST;
            }  else if (PropertyConstant.CONF_PROD_UAT.equals(env)) {
                lcaUrl = LCA_URL_UAT;
            }   else if (PropertyConstant.CONF_PROD_PRE.equals(env)) {
                lcaUrl = LCA_URL_PRE;
            } else {
                lcaUrl = LCA_URL_TEST;
            }
            String result = HttpUtil.doPost(json.toJSONString(), new HashMap<>(), lcaUrl);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            return data.getLong("bizDateTime");
        } catch (IOException e) {
            logger.error("获取lca业务时间失败", e);
        }
        return null;
    }
    
    public static Long getLsaBizTime() {
        try {
            String url = null;
            String env = System.getProperty(PropertyConstant.SPRING_PROFILES_ACTIVE);
            if (PropertyConstant.CONF_PROD_DEV.equals(env)) {
                url = LSA_URL_DEV;
            } else if (PropertyConstant.CONF_PROD_TEST.equals(env)) {
                url = LSA_URL_TEST;
            }  else if (PropertyConstant.CONF_PROD_UAT.equals(env)) {
                url = LSA_URL_UAT;
            }   else if (PropertyConstant.CONF_PROD_PRE.equals(env)) {
                url = LSA_URL_PRE;
            } else {
                url = LSA_URL_TEST;
            }
            String result = HttpUtil.doGet(null, url);
            JSONObject jsonObject = JSON.parseObject(result);
            JSONObject data = jsonObject.getJSONObject("data");
            return data.getLong("bizTime");
        } catch (Exception e) {
            logger.error("获取lsa业务时间失败", e);
        }
        
        return null;
    }
}
